<?php
// +----------------------------------------------------------------------
// | YFCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: rainfer <81818832@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\Model;
use app\admin\controller\Auth;

/**
 * 后台菜单模型
 * @package app\admin\model
 */
class AuthDoc extends Model
{
    /**
     * 权限检测
     * @param int
     * @return boolean
     */
    public function check_auth($doc_id, $action="read")
    {   
        $uid = session('userinfo.id');
        $auth_ids_list = cache('auth_doc_'.$action.'_list_'.$uid);
        
        if (empty($auth_ids_list)) {
            $auth_ids_list = $this->getAuthList($uid,$action);
            cache('auth_doc_'.$action.'_list_'.$uid,$auth_ids_list);
        }
        //var_dump($auth_ids_list);
        //exit;
        if(empty($auth_ids_list)) return false;
        
        if (in_array($doc_id, $auth_ids_list)) {
            return true;
        } else {
            return false;
        }
    }
    
    
    public function getAuthList($uid,$action='read')
    {   
        $auth_ids = db('auth_doc')->alias('a')->join('auth_group_access b','b.group_id = a.group_id')
                ->where(['a.'.$action=>1,'b.uid'=>$uid])
                ->column('a.doc_id');

        return $auth_ids = !empty($auth_ids)?array_unique($auth_ids):[];  
    }
}